package com.lengyue.imakiba.show;

import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.lengyue.imakiba.data.Anime;
import com.lengyue.imakiba.data.Type;

public class Search extends Controller {
	public void index()
	{
		int cvId = getPara("cvId")==null?0:Integer.parseInt(getPara("cvId"));
		String typeCode = getPara("typeCode");
		String search = getPara("search");
		String state = getPara("state");
		//动漫类型
		List<Type> types = Type.dao.find("select * from type order by sort_no");
		setAttr("types", types);
		setAttr("cvId", cvId);
		setAttr("typeCode", typeCode);
		setAttr("search", search);
		setAttr("state", state);
		render("/WEB-INF/freeMarkerTemplete/search_result.ftl");
	}
	/**
	 * 查询动漫
	 * @param typeCode 动漫类型
	 * @param search 匹配文本
	 * @return 匹配的动漫
	 */
	public void getSearchResult()
	{
		String typeCode = getPara("typeCode");
		String search = getPara("search");
		String state = getPara("state");
		int cvId = getPara("cvId")==null?0:Integer.parseInt(getPara("cvId"));
		int page = getParaToInt("page");
		int length = getParaToInt("length");
		Page<Anime> animes = null;
		/*if(typeCode!=null&&!"".equals(typeCode)&&search!=null&&!"".equals(search))
		{
			String select = "select DISTINCT a.* ";
			String sqlExceptSelect = "from anime a "
					+"INNER JOIN anime_type at on a.id = at.anime_id "
					+"where at.type_code = ? and a.code like CONCAT('%',?,'%') "
					+"or name like CONCAT('%',?,'%') order by a.id desc";
			animes = Anime.dao.paginate(page, length, select, sqlExceptSelect, typeCode,search,search);
		}else */
		if(typeCode!=null&&!"".equals(typeCode))
		{
			String select = "select DISTINCT a.* ";
			String sqlExceptSelect = "from anime a "
					+"INNER JOIN anime_type at on a.id = at.anime_id "
					+"where at.type_code = ? order by a.id desc";
			animes = Anime.dao.paginate(page, length, select, sqlExceptSelect, typeCode);
		}
		if(search!=null&&!"".equals(search))
		{
			String select = "select * ";
			String sqlExceptSelect = "from anime where code like CONCAT('%',?,'%') or name like CONCAT('%',?,'%') order by id desc";
			animes = Anime.dao.paginate(page, length, select, sqlExceptSelect,search,search);
		}
		if(cvId!=0)
		{
			animes =  Anime.dao.paginate(page, length,"select a.* ","from anime a inner join anime_cv ac on a.id = ac.anime_id where cv_id = ? order by a.id desc",cvId);
		}
		if(state!=null&&!"".equals(state))
		{
			animes =  Anime.dao.paginate(page, length,"select * ","from anime where state = ? order by id desc",state);
		}
		renderJson(animes.getList());
	}
	/**
	 * 查询声优的动漫
	 * @param page 页号
	 * @param length 每页个数
	 * @param cvId 声优Id
	 *//*
	public void getAnimesByCV()
	{
		int cvId = getParaToInt("cvId");
		int page = getParaToInt("page");
		int length = getParaToInt("length");
		List<Anime> animes =  Anime.dao.paginate(page, length,"select a.* ","from anime a inner join anime_cv ac on a.id = ac.anime_id where cv_id = ? order by a.id desc",cvId).getList();
		renderJson(animes);
	}*/
}
